#include "stdafx.h"
#include "GameBall.h"
#include "Game.h"
#include "PlayerPaddle.h"
#include "ServiceLocator.h"


GameBall::GameBall() :
	_velocity(220.0f),
	_elapsedTimeSinceStart(0.0f)
{
	Load("images/ball.png");
	assert(IsLoaded());

	GetSprite().setOrigin(15, 15);

	_angle = rand() % 360;

	_font.loadFromFile("calibri.ttf");
	_text.setFont(_font);
	_text.setCharacterSize(24);
	_text.setColor(sf::Color::Blue);
}


GameBall::~GameBall()
{
}

void GameBall::ResetTime()
{
	_elapsedTimeSinceStart = 0;
}

void GameBall::Draw(sf::RenderWindow& window)
{
	if (IsLoaded())
	{
		window.draw(GetSprite());
		window.draw(_text);
	}
}

void GameBall::collideWith(CollidableObject* collisionObject)
{
	float playerVelocity;
	AIPaddle* aiPaddle = dynamic_cast<AIPaddle*>(collisionObject);
	PlayerPaddle* playerPaddle = dynamic_cast<PlayerPaddle*>(collisionObject);
	if (playerPaddle != NULL)
	{
		playerVelocity = playerPaddle->GetVelocity();
	}
	else if (aiPaddle != NULL)
	{
		playerVelocity = aiPaddle->GetVelocity();
	}
	if (playerPaddle != NULL || aiPaddle != NULL)
	{
		ServiceLocator::GetAudio()->PlaySound("audio/kaboom.wav");
		_angle =  _angle - 90;
		if (_angle > 360.0f)
		{
			_angle -= 360.0f;
		}

		_moveByY = -2 * _moveByY; //revert change and go opposite direction
		_moveByX = -2 * _moveByX;

		if (playerVelocity < 0)
		{
			_angle -= 20.0f;
			if (_angle < 0)
			{
				_angle = 360.0f - _angle;
			}
		}
		else if (playerVelocity > 0)
		{
			_angle += 20.0f;
			if (_angle > 360.0f)
			{
				_angle = _angle - 360.0f;
			}
		}
		_velocity += 15.0f;
		GetSprite().move(_moveByX, _moveByY);
	}
}

void GameBall::Update(float elapsedTime)
{
	_elapsedTimeSinceStart += elapsedTime;

	//Delay game from starting until 3 seconds pass
	if (_elapsedTimeSinceStart < 1.0f)
		return;

	float moveAmount = _velocity * elapsedTime;
	sf::Vector2f pos = GetPosition();
	
	_moveByX = LinearVelocityX(_angle) * moveAmount;
	_moveByY = LinearVelocityY(_angle) * moveAmount;

	//Check screen collision
	if (pos.x + _moveByX <= GetWidth() / 2 ||
		pos.x + GetWidth() / 2 + _moveByX >= Game::SCREEN_WIDTH)
	{
		_angle = 180 - _angle;
		if (_angle > 260.0f && _angle < 280.0f) _angle += 20.0f;
		if (_angle > 80.0f && _angle < 100.0f) _angle += 20.0f;
		_moveByX = -_moveByX;
	}

	if (pos.y + _moveByY <= (GetHeight() / 2))
	{
		// Passed AI Paddle player scored
		ServiceLocator::GetScore()->GivePlayerPoint();
		GetSprite().setPosition(Game::SCREEN_WIDTH / 2, Game::SCREEN_HEIGHT / 2);
		_angle = rand() % 360;
		_velocity = 220.0f;
		_elapsedTimeSinceStart = 0.0f;
	}

	else if (pos.y + GetHeight() / 2 + _moveByY >= Game::SCREEN_HEIGHT)
	{
		//move to middle of screen
		ServiceLocator::GetScore()->GiveAIPoint();
		GetSprite().setPosition(Game::SCREEN_WIDTH / 2, Game::SCREEN_HEIGHT / 2);
		_angle = rand() % 360;
		_velocity = 220.0f;
		_elapsedTimeSinceStart = 0.0f;
	}
	GetSprite().move(_moveByX, _moveByY);
	std::ostringstream oss;
	oss << "Time:" << elapsedTime;
	_text.setString(oss.str());
}

float GameBall::LinearVelocityX(float angle) const
{
	return (float)std::cos(angle * (3.1415926 / 180.0f));
}
float GameBall::LinearVelocityY(float angle) const
{
	//negative Y component to rotate 90 degrees
	return -((float)std::sin(angle * (3.1415926 / 180.0f)));
}

float GameBall::GetVelocityX() const
{
	return LinearVelocityX(_angle);
}
float GameBall::GetVelocityY() const
{
	return LinearVelocityY(_angle);
}

void GameBall::Pause(bool isPaused)
{
	VisibleGameObject::Pause(isPaused);
	_elapsedTimeSinceStart = 0;
}
